import util from "../../util";

const api = util.api.Schedule  // Schedule
export default class scheduleModelCollection {
    constructor() {
        this.util=util;
        this.info=[];
        this.infoTree=[];
        this.infoCascader=[];
        this.list=[];
        this.listTree=[];
        this.listCascader=[];
        this.records=[];
        this.recordsTree=[];
        this.recordsCascader=[];
        this.total=0;
    }
    static create(){
        let object=new scheduleModelCollection();
        return object;
    }
    /**
     * 批量删除
     * */
    async batchDelete(filter){
        let result = await api.batchDelete(filter).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;

    }
    async deleteByIds(ids){
        let result = await api.deleteByIds(ids).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;

    }
    /**
     * 条件查询，当前版本，不分页
     * */
    async query(filter){
        let result = await api.query({condition:filter}).then(data=>{
            if(!!data) {
                this.records = data;
                this.recordsTree = util.getTree(data, 'category', 'name')
                this.recordsCascader = util.getCascader(data, 'category', 'name')
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 条件查询，当前版本，分页
     * */
    async queryByPage(pageNumber, pageSize, filter){
        let result = await api.queryByPage(pageNumber, pageSize, {condition:filter}).then(data=>{
            if(!!data) {
                this.records = data.records;
                this.total = data.recordCount;
                this.recordsTree = util.getTree(data.records, 'category', 'name')
                this.recordsCascader = util.getCascader(data.records, 'category', 'name')
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /********************************************************************************************
     * 摘要查询
     * ******************************************************************************************/
    /**
     * 摘要查询，当前版本，不分页
     * */
    async listAllInfo(){
        let result = await api.queryInfo({}).then(data=>{
            if(!!data) {
                this.info = data;
                this.infoTree = util.getTree(data, 'category', 'name');
                this.infoCascader = util.getCascader(data, 'category', 'name');
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 摘要查询，当前版本，分页
     * */
    async queryInfoByPage(pageNumber, pageSize,filter){
        let result = await api.queryInfoByPage(pageNumber, pageSize, {condition:filter}).then(data=>{
            if(!!data) {
                this.records = data.records;
                this.total = data.recordCount;
                this.recordsTree = util.getTree(data.records, 'category', 'name')
                this.recordsCascader = util.getCascader(data.records, 'category', 'name')
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }

    /********************************************************************************************
     * 版本管理
     * ******************************************************************************************/
    /**
     * 摘要查询，所有版本，不分页
     * */
    async listAllVersionInfo(){
        let result = await api.queryInfoIncludeUnpublished({}).then(data=>{
            if(!!data) {
                this.info = data;
                this.infoTree = util.getTree(data, 'category', 'name');
                this.infoCascader = util.getCascader(data, 'category', 'name');
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /**
     * 全部查询，所有版本，分页
     * */
    async listAllVersion(){
        let result = await api.queryIncludeUnpublished({}).then(data=>{
            if(!!data) {
                this.list = data;
                this.listTree = util.getTree(data, 'category', 'name')
                this.listCascader = util.getCascader(data, 'category', 'name')
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
}